/**
 * 会议日历
 */

 import moment from 'moment';
 import { postAction } from '@/api/manage'
 import resourceTimelinePlugin from '@fullcalendar/resource-timeline';
 import interactionPlugin from '@fullcalendar/interaction'
 import { mapGetters } from 'vuex';
 export const CalendarMixin = {
   data() {
     return {
       calendarPlugins: [
         resourceTimelinePlugin, interactionPlugin
       ],
       currentView: 'resourceTimelineMonth',
       eventDataSource: [],
       eventMaps: {},
       item: {},
       buttonText: {
         today: '今天',
         resourceTimelineMonth: '月',
         resourceTimelineWeek: '周',
         resourceTimelineDay: '日'
       },
       // 点击今天日列表图
       eventLimitClick: 'resourceTimelineDay',
       // 隐藏超出的事件
       eventLimit: true,
       firstDay: 0,
       header: {
         left: 'prev,next, today',
         center: 'title',
         right: 'resourceTimelineDay,resourceTimelineWeek,resourceTimelineMonth'
       },
       licenseKey: 'GPL-My-Project-Is-Open-Source',
       aspectRatio: 2.4,
       handlerSelectAllow: info => {
				return true
        //  const currentDate = new Date()
        //  const start = info.start
        //  const end = info.end
        //  const viewAllow = this.currentView == 'resourceTimelineDay'
        //  return (start <= end && start >= currentDate && viewAllow && this.adleEdit)
       },
       resources: [],
       calendarEventsNew: []
     }
   },
   created() {
			this.loadRoom() // 获取数据
   },
   watch: {
     resources(val) {
       this.$emit('change', val)
     },
     calendarParam: {
      handler (val) {
        if (val) {
          this.loadRoom()
        }
      },
      immediate: true,
      deep: true
     }
   },
   methods: {
     moment,
     loadRoom() {
			postAction('/api/erp/erpcarusing/erpCarUsing/getUsingCarInfo', {
				carType: this.calendarParam.applyUser || '',
				createBy: this.calendarParam.documentNo || '',
				time: this.calendarParam.createTime || ''
       }).then(res => {
         console.log('loadRoom==>res.result', res.result)
         if (res.success) {
           let resources = []
           if (res.result != null) {
						res.result.records[0].erpCarInfoList.map(value => {
							let obj = {};
							obj = { ...value }
              obj.id = value.id;
              obj.title = value.carName + value.carNum + ' ' + '-' + ' ' + value.carAdmin_dictText;
              if (value.carState && value.carState == 1) {
                obj.selectable = true
              } else {
                obj.selectable = false
              }
              resources.push(obj)
						})
           }
           this.resources = resources;
           console.log('this.resources', this.resources)
           if (this.resources == null || this.resources.length == 0) {
             let calendarApi = this.$refs.baseCalendar.getApi() // 通过getApi()方法获取
            //  console.log('calendarApi', calendarApi)
           /*  if(this.currentView=='resourceTimelineDay'){
               calendarApi.changeView("resourceTimelineMonth")
             }else{
               calendarApi.changeView("resourceTimelineDay")
             } */
           }
          //  console.log('loadRoom==>this.resources', this.resources)
           this.loadMetting(res.result.records[0].erpCarUsingList)
         }
       })
     },
     loadMetting(res) {
			     this.eventDataSource = res
					 let eventMaps = {}
           if (res && res.length) {
            res.forEach(item => eventMaps[item.carId] = item)
           }
           
           this.eventMaps = eventMaps
          //  console.log('this.eventMaps==>', this.eventMaps)
           this.renderCalendar()
     },
     renderCalendar(currentView) {
       // TODO 判断修改重复的event
       this.calendarEventsNew = this.eventDataSource.map(item => {
        //  console.log("item===>",item
         let event = {
           id: item.id,
           empName: item.createBy,
           carAdmin: item.carAdmin,
           carAdmin_dictText: item.carAdmin_dictText,
           title: item.createBy + ' ' + item.applyUnit_dictText,
           resourceId: item.carId,
           start: item.usingStartTime,
           end: item.usingEndTime,
           carId_dictText: item.carName,
           color: '#dba615', // TODO 增加颜色，从后台获取,
         }
         return event
       })
			//  console.log('this.calendarEventsNew', this.calendarEventsNew)
     },
     datesRender(info) {
       this.currentView = info.view.type;
     },
     resourceRender(info) {
       console.log('resourceRender触发。', info)
     },
     handleEventClick(info) {
			console.log('handleEventClick', info)
      if (this.userInfo().empName !== info.event.extendedProps.empName) {
        return false
      }
       if (this.adleEdit) {
         this.$refs.mettingModal.edit(info)
       }
     },
     ...mapGetters(['userInfo']),
     select: function(info) {
      console.log('userInfo', this.userInfo())
			console.log('select', info)
      this.$refs.mettingModal.add(info)
      //  if (this.adleEdit) {
      //    this.$refs.mettingModal.add(info)
      //  }
     },
     eventEnter(info) {
			// console.log('eventEnter')
       this.tranLeft = (info.jsEvent.pageX - 490) + 'px';
       this.tranTop = (info.jsEvent.pageY - 155) + 'px';
       this.$nextTick(() => {
         this.item = info.event
       })
      //  点击的时候显示模态框
       this.showBox = true;
     },
     eventLeave(info) {
			// console.log('eventLeave')
       this.showBox = false
     }
   }

 }
